--SELECT DBO.FN_GET_BINS_USED_FOR_ASSEMBLY_RELEASE (4553,3) AS BIN_INFO
alter FUNCTION fn_get_bins_used_for_assembly_release(@LOT_ID INT, @WAFER_ID INT) RETURNS VARCHAR(1000)
AS
BEGIN
	DECLARE @BIN_INFO VARCHAR(200)
	DECLARE @ARLT_CURSOR CURSOR
	DECLARE @ARLT_BIN_INFO VARCHAR(200)
	DECLARE @SP_CHAR_IDX INT
	DECLARE @ARLT_BIN VARCHAR(10)
	DECLARE @SP_CHAR_IDX_FOR_BIN_INFO INT
		

	SET @BIN_INFO = '&'
	SET @ARLT_CURSOR = CURSOR FOR SELECT REPLACE(UPPER(ASLT.BIN_INFO), 'BIN','') FROM ASSM_RELEASE_LOT_TABLE ASLT, ASSM_RELEASE_TABLE ART WHERE ART.ASSEMBLY_RELEASE_ID = ASLT.ASSEMBLY_RELEASE_ID AND ART.STATUS <> 'CANCELLED' AND ASLT.LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
	OPEN @ARLT_CURSOR
	FETCH FROM @ARLT_CURSOR INTO @ARLT_BIN_INFO
	WHILE( @@FETCH_STATUS = 0 )
	BEGIN
		SET @ARLT_BIN_INFO = @ARLT_BIN_INFO + '&'
		SET @SP_CHAR_IDX = CHARINDEX( '&', @ARLT_BIN_INFO )
		WHILE( @SP_CHAR_IDX <> 0 )
		BEGIN
			SET @ARLT_BIN = SUBSTRING( @ARLT_BIN_INFO, 1, @SP_CHAR_IDX )
			SET @SP_CHAR_IDX_FOR_BIN_INFO = CHARINDEX( '&'+@ARLT_BIN, @BIN_INFO)						
			IF( @SP_CHAR_IDX_FOR_BIN_INFO = 0 ) 
			BEGIN
				SET @BIN_INFO = @BIN_INFO + @ARLT_BIN
			END
			SET @ARLT_BIN_INFO = SUBSTRING( @ARLT_BIN_INFO, @SP_CHAR_IDX+1, LEN(@ARLT_BIN_INFO) )
			SET @SP_CHAR_IDX = CHARINDEX( '&', @ARLT_BIN_INFO )						
		END
		FETCH FROM @ARLT_CURSOR INTO @ARLT_BIN_INFO		
	END
	RETURN @BIN_INFO
END
